package com.app.webcamwidgetca;

import java.util.Random;

import com.app.webcamwidgetca.*;
import com.app.webcamwidgetca.WordWidget.UpdateService;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * A list view example where the 
 * data for the list comes from an array of strings.
 */
public class Conf extends Activity {


	public static final String PREFS_NAME = "PrefWidgetWebcam";
	private String urlString;
	private Button ValideButton;
	private EditText champUrl;
	private ImageView imgPreview;
	//private int mWidgetId;
	private Uri mData;
	private boolean firstConf = true;

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuration);

        //Intent
        
        //WordWidget.idw = getIntent().getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
        //mWidgetId = getIntent().getFlags();
        mData = getIntent().getData();

     // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        urlString = settings.getString(mData.toString(), "");
      String filenameString =  settings.getString(mData.toString()+"f", "");
      if(filenameString!="") firstConf = false; 
      
      
        champUrl = (EditText) findViewById(R.id.EditText01);
        imgPreview = (ImageView) findViewById(R.id.ImageView01);
        //if(WordWidget.bitmap!=null) imgPreview.setImageBitmap(WordWidget.bitmap);
        Bitmap bitmap;
        bitmap = BitmapFactory.decodeFile("/data/data/com.app.webcamwidgetca/files/"+filenameString);
        if(bitmap != null)
        	{
        	imgPreview.setImageBitmap(bitmap);
        	}
        
        
        
        champUrl.setText(urlString);

       
       this.setTitle(this.getTitle() );//+" "+ String.valueOf(mWidgetId)
       
        ValideButton = (Button) findViewById(R.id.Button01);
        ValideButton.setOnClickListener(listenerClick);
         
   }

  OnClickListener listenerClick  = new OnClickListener() {
  	@Override
  	public void onClick( View v) {
  		// TODO Auto-generated method stub
  		urlString = champUrl.getText().toString();
  		//Intent updateWidget = new Intent(android.appwidget.action.APPWIDGET_UPDATE);
  		//sendBroadcast(updateWidget);

  		
  		
  		finish();

  	}
  };
    
    @Override
    protected void onStop(){
       super.onStop();
    


       
      // Save user preferences. We need an Editor object to
      // make changes. All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString(mData.toString(),urlString);// "urlww"+ String.valueOf(mWidgetId),urlString);
      if(firstConf==true)
      {
      Random randomizer = new Random(System.currentTimeMillis());  
      editor.putString(mData.toString()+"f",String.valueOf(randomizer.nextInt(9999))+ ".png");
      }
      // Don't forget to commit your edits!!!
      editor.commit();
      
      // Push update for this widget to the home screen
      //ComponentName thisWidget = new ComponentName(this,WordWidget.class);
      //AppWidgetManager manager = AppWidgetManager.getInstance(this);
      //manager.updateAppWidget(thisWidget, updateViews);
      Context context = getApplicationContext();
      context.startService(new Intent(context, UpdateService.class));
    }

    
    
    @Override
    public void onResume() {
        super.onResume();

      
        }  

}